package cn.icanci.loopstack.bic.dal.mongo.mongo;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import cn.icanci.loopstack.bic.dal.mongo.common.PageList;
import cn.icanci.loopstack.bic.dal.mongo.daointerface.SystemDAO;
import cn.icanci.loopstack.bic.dal.mongo.dateobject.SystemDO;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/10 15:29
 */
@Service("systemDAO")
public class MongoSystemDAO extends AbstractBaseDAO<SystemDO> implements SystemDAO {
    @Override
    public void insert(SystemDO system) {
        super.insert(system);
        mongoTemplate.insert(system, COLLECTION_NAME);
    }

    @Override
    public void update(SystemDO system) {
        super.update(system);
        mongoTemplate.save(system, COLLECTION_NAME);
    }

    @Override
    public List<SystemDO> queryAll() {
        Criteria criteria = Criteria.where(SystemColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.find(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public PageList<SystemDO> pageQuery(SystemDO system, int pageNum, int pageSize) {
        Criteria criteria = Criteria.where(SystemColumn.ENV).is(DEFAULT_ENV);
        if (StringUtils.isNotBlank(system.getSystemKey())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(SystemColumn.SYSTEM_KEY).regex("^.*" + system.getSystemKey() + ".*$", "i");
        }
        if (StringUtils.isNotBlank(system.getSystemName())) {
            // 不分区大小写查询，其中操作符"i"：表示不分区大小写
            criteria.and(SystemColumn.SYSTEM_NAME).regex("^.*" + system.getSystemName() + ".*$", "i");
        }
        Query query = new Query(criteria);
        query.with(Sort.by(Sort.Direction.DESC, SystemColumn.CREATE_TIME));
        return pageQuery(query, COLLECTION_CLASS, pageSize, pageNum, COLLECTION_NAME);
    }

    @Override
    public SystemDO queryOneById(String _id) {
        Criteria criteria = Criteria.where(SystemColumn._ID).is(_id);
        criteria.and(SystemColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public Class<? extends BaseColumn> getBaseColumn() {
        return SystemColumn.class;
    }

    @Override
    public SystemDO queryByName(String name) {
        Criteria criteria = Criteria.where(SystemColumn.SYSTEM_NAME).is(name);
        criteria.and(SystemColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public SystemDO queryByKey(String key) {
        Criteria criteria = Criteria.where(SystemColumn.SYSTEM_KEY).is(key);
        criteria.and(SystemColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override public SystemDO queryByUuid(String systemUuid) {
        Criteria criteria = Criteria.where(SystemColumn.UUID).is(systemUuid);
        criteria.and(SystemColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }
}
